<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.phone"
        placeholder="手机号"
        style="width: 200px;"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.vehicle"
        placeholder="车牌号"
        style="width: 200px;"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.bankCard"
        placeholder="银行卡号"
        style="width: 200px;"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.bankIdCard"
        placeholder="身份证号"
        style="width: 200px;"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >{{ $t('table.search') }}</el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >{{ $t('table.add') }}</el-button>
      <!-- <el-button v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">
        {{ $t('table.export') }}
      </el-button>
      <el-checkbox v-model="showReviewer" class="filter-item" style="margin-left:15px;" @change="tableKey=tableKey+1">
        {{ $t('table.reviewer') }}
      </el-checkbox>-->
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column label="序号" prop="id" align="center" width="50">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column label="姓名" min-width align="center">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="电话" min-width align="center">
        <template slot-scope="scope">{{ scope.row.phone }}</template>
      </el-table-column>
      <el-table-column label="车牌" min-widthh align="center">
        <template slot-scope="scope">{{ scope.row.vehicle }}</template>
      </el-table-column>
      <el-table-column label="银行卡号" width align="center">
        <template slot-scope="scope">{{ scope.row.bankCard }}</template>
      </el-table-column>
      <el-table-column label="银行" min-width align="center">
        <template slot-scope="scope">{{ scope.row.type | typeFilter }}</template>
      </el-table-column>
      <el-table-column label="银行姓名" min-width align="center">
        <template slot-scope="scope">{{ scope.row.bankUserName }}</template>
      </el-table-column>
      <el-table-column label="银行身份证" min-width align="center">
        <template slot-scope="scope">{{ scope.row.bankIdCard }}</template>
      </el-table-column>
      <el-table-column label="类型" min-width align="center">
        <template slot-scope="scope">{{ scope.row.bankType }}</template>
      </el-table-column>
      <el-table-column label="状态" min-width align="center">
        <template slot-scope="scope">{{ scope.row.state| stateFilter }}</template>
      </el-table-column>
      <el-table-column label="操作人" min-width align="center">
        <template slot-scope="scope">{{ scope.row.executor }}</template>
      </el-table-column>
      <el-table-column label="创建时间" min-width align="center">
        <template slot-scope="scope">{{ scope.row.created }}</template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="170px"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleGetMoney(scope.row)">提现</el-button>
          <el-button type="danger" size="mini" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
    <el-dialog title="新增" :visible.sync="dialogFormVisible" top="2vh" width="600px">
      <el-form
        ref="temp"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="140px"
        style="width: 500px; margin-left:30px;"
      >
        <el-form-item label="用户手机号" prop="phone">
          <el-input v-model="temp.phone" />
        </el-form-item>
        <el-form-item label="银行卡号" prop="bankCard">
          <el-input v-model="temp.bankCard" clearable />
        </el-form-item>
        <el-form-item label="开户人姓名" prop="bankUserName">
          <el-input v-model="temp.bankUserName" clearable />
        </el-form-item>
        <el-form-item label="开户人身份证号码" prop="bankIdCard">
          <el-input v-model="temp.bankIdCard" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button type="primary" @click="createData()">{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>
    <el-dialog title="提现" :visible.sync="dialogFormMoney" top="2vh" width="600px">
      <el-form
        ref="withdrawForm"
        :rules="withdrawFormRules"
        :model="withdrawForm"
        label-position="left"
        label-width="100px"
        style="width: 300px; margin-left:50px;"
        class="demo-dynamic"
      >
        <el-form-item label="可提现金额" prop="walletTotal">
          <el-input v-model="withdrawForm.walletTotal" :disabled="true" />
        </el-form-item>
        <el-form-item label="提现金额" prop="money">
          <el-input v-model="withdrawForm.money" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormMoney = false">{{ $t('table.cancel') }}</el-button>
        <el-button type="primary" @click="withdraw()">{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import permission from "@/directive/permission/index.js"; // 权限判断指令
import checkPermission from "@/utils/permission"; // 权限判断函数

import { fetchPv, updateCooperation } from "@/api/article";
import {
  cooperationList,
  createCooperation,
  editPay,
  getCode,
  getallAgent,
  cooperationAll
} from "@/api/v1";
import { userBanks } from "@/api/user";
import { forList } from "@/api/get-list";
import { post, get } from "@/api/v1";
import waves from "@/directive/waves"; // Waves directive
import { parseTime } from "@/utils";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
import qrCode from "@/components/qrcode";
import { Loading } from "element-ui";
import provinceData from "@/components/province";
import addUsers from "@/components/addUsers";

export default {
  name: "ComplexTable",
  components: { Pagination, qrCode, provinceData, addUsers },
  directives: { waves, permission },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "info",
        deleted: "danger"
      };
      return statusMap[status];
    },
    typeFilter(type) {
      if (type == 1) {
        return "加油站";
      } else if (type == 2) {
        return "修理站";
      } else if (type == 3) {
        return "加气站";
      } else if (type == 4) {
        return "总公司";
      } else {
        return "";
      }
    },
    stateFilter(state) {
      switch (state) {
        case 0:
          return "已删除";
          break;
        case 1:
          return "审核中";
          break;
        case 2:
          return "验证失败";
          break;
        case 3:
          return "成功";
          break;
        case 4:
          return "人工";
          break;
        default:
          return "未知状态";
      }
    }
  },
  data() {
    return {
      rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        bankCard: [
          { required: true, message: "请输入银行卡号", trigger: "change" }
        ],
        phone: [{ required: true, message: "请输入电话", trigger: "change" }],
        bankUserName: [
          { required: true, message: "请输入开户人", trigger: "change" }
        ],
        bankIdCard: [
          { required: true, message: "请输入身份证号码", trigger: "change" }
        ],
        money: [{ required: true, message: "请输入金额", trigger: "change" }]
      },
      withdrawFormRules: {
        bankCard: [
          { required: true, message: "请输入银行卡号", trigger: "change" }
        ],
        money: [{ required: true, message: "请输入金额", trigger: "change" }]
      },
      imageUrl: "",
      codeVal: "",
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      dialogFormMoney: false,
      listQuery: {
        page: 1,
        limit: 20,
        phone: "",
        vehicle: "",
        bankCard: "",
        sort: "+id",
        state: "",
        bankIdCard: ""
      },
      showReviewer: false,
      temp: {
        phone: "",
        bankCard: "",
        bankUserName: "",
        bankIdCard: ""
      },
      withdrawForm: {
        walletTotal: "",
        money: "",
        userId: "",
        bankCard: ""
      },
      temp1: {
        remainder: "",
        money: ""
      },

      cooperation: {},

      dialogFormVisible: false,
      dialogCode: false,
      dialogPay: false,
      dialogStatus: "",
      textMap: {
        update: "编辑",
        create: "新增"
      },
      dialogPvVisible: false,
      pvData: [],
      downloadLoading: false,
      isAdmin: true
    };
  },
  created() {
    this.getList();
  },
  methods: {
    del(row) {
      this.$confirm("此操作将删除该银行卡 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        post(this.API.URL.BANK_CARDS_DEL, row).then(res => {
          this.$message({
            message: res.message,
            type: "success",
            duration: 2 * 1000
          });
        });
      });
    },
    withdraw() {
      console.log(this.withdrawForm);
      // this.temp1.bankCardId = this.temp1.id;
      this.withdrawForm.bankCardId = this.temp1.id;
      this.$refs["withdrawForm"].validate(valid => {
        if (valid) {
          post(this.API.URL.WALLET_WITHDRWA, this.withdrawForm).then(res => {
            this.dialogFormMoney = false;
            this.$message({
              message: res.message,
              type: "success",
              duration: 2 * 1000
            });
          });
        }
      });
    },
    handleGetMoney(row) {
      this.temp1 = Object.assign({}, row); // copy obj
      console.log(row);
      console.log(this.temp1);
      let query = { userId: row.userId };
      get(this.API.URL.WALLET_TOTAL_MONEY, query).then(res => {
        this.withdrawForm.walletTotal = res.data.total;
      });

      this.withdrawForm.bankCard = row.bankCard;
      this.withdrawForm.userId = row.userId;

      this.dialogFormMoney = true;
      this.$nextTick(() => {
        this.$refs["withdrawForm"].clearValidate();
      });
    },

    getList() {
      this.listLoading = true;
      forList(this.API.URL.BANK_CARDS_LIST, this.listQuery).then(response => {
        this.list = response.data.records;
        this.total = Number(response.data.total);

        this.listLoading = false;
      });
    },
    resetTemp() {
      this.temp = {
        phone: "",
        bankCard: "",
        bankUserName: "",
        bankIdCard: ""
      };
    },

    handleCreate() {
      this.resetTemp();
      console.log(this.temp);
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["temp"].clearValidate();
      });
    },

    createData() {
      this.$refs["temp"].validate(valid => {
        if (valid) {
          post(this.API.URL.BANK_CARDS_ADD, this.temp)
            .then(res => {
              // this.list.unshift(this.temp);
              this.dialogFormVisible = false;
              this.$notify({
                title: "成功",
                message: res.message,
                type: "success",
                duration: 2000
              });
              this.getList();
            })
            .catch(() => {});
        }
      });
    },
    handleUpdate(row) {
      this.resetTemp();
      this.temp = Object.assign({}, row); // copy obj
      console.log(row);
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
    },

    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    sortChange(data) {
      const { prop, order } = data;
      if (prop === "id") {
        this.sortByID(order);
      }
    }
  }
};
</script>


